import axios from 'axios';
import {getStore, setStore} from './storage';
import Cookies from 'js-cookie';
import Toast from 'muse-ui-toast';
import router from '../router'

let env = process.env.NODE_ENV;

export const base = env === 'development'
  ? 'http://106.12.87.23' +
  '4:8086/creditor/share'
  // ? 'http://192.168.3.125:8086/creditor/share'
  : env === 'production'
    ? 'http://106.12.87.234:8086/creditor/share'
    : 'http://192.168.0.126:8083/creditor';

export const socketBase = env === 'development'
  ? 'http://127.0.0.1:8082/receiver'
  // ? 'http://192.168.0.126:8082/receiver'
  : env === 'production'
    ? 'http://182.61.32.24:8084/socket'
    : 'http://192.168.0.126:8082/creditor';

export const creditorBase = env === 'development'
  ? 'http://127.0.0.1:8083/creditor'
  : env === 'production'
    ? 'http://web.zj.icbc.com.cn/creditor'
    : 'http://192.168.0.5:8083/creditor';

export const WEB_SOCKET_SERVICE = socketBase + "/endpointWisely";
export const WEB_SOCKET_MEETING = creditorBase + "/endpoint";
// 超时设定
axios.defaults.timeout = 20000;

axios.interceptors.request.use(config => {
  return config;
}, err => {
  Toast.error('请求超时');
  return Promise.resolve(err);
});

// http response 拦截器
axios.interceptors.response.use(response => {
  const data = response.data;
  // 根据返回的status值来做不同的处理(和后端约定)
  switch (data.code) {
    case 401:
      // 无权限 清除token
      Cookies.set('userInfo', '');
      setStore('accessToken', '');
      break;
    case 403:
      // 未登录 清除已登录状态
      Cookies.set('userInfo', '');
      setStore('accessToken', '');
      Toast.success("用户未登陆,2秒后系统将跳转登陆页面");
      setTimeout(() => {
        router.push({name: 'login'});
      }, 2000);
      break;
    case 500:
      // 错误
      if (data.message) {
        Toast.error(data.message);
      } else {
        Toast.error("服务器异常");
      }
      break;
    case 21:
      // 错误
      if (data.message) {
        Toast.info(data.message);
      } else {
        Toast.info("业务异常");
      }
      break;
    case 200:
      return data;
    default:
      Toast.error(data.message);
      break;
  }
  return data;
}, (err) => { // 这里是返回状态码不为200时候的错误处理
  Toast.error(err.toString());
  return Promise.resolve(err);
});

export const postRequest = (url, params) => {
  let accessToken = getStore("accessToken");
  let declareToken = getStore("declareToken");
  let registerToken = getStore("registerToken");
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      return JSON.stringify(data);
    }],
    headers: {
      'Content-Type': 'application/json',
      'token': accessToken,
      'ref-declare-token': declareToken,
      "ref-register-token": registerToken
    }
  });
};

export const socketPostRequest = (url, params) => {
  let accessToken = getStore("accessToken");
  let declareToken = getStore("declareToken");
  let registerToken = getStore("registerToken");
  return axios({
    method: 'post',
    url: `${socketBase}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = '';
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
      }
      return ret;
    }],
    headers: {
      'Content-Type': 'application/json',
      'token': accessToken,
      'ref-declare-token': declareToken,
      "ref-register-token": registerToken
    }
  });
};

export const uploadFileRequest = (url, params) => {
  let accessToken = getStore('accessToken');
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data',
      'token': accessToken
    }
  });
};

export const deleteRequest = (url, params) => {
  let accessToken = getStore('accessToken');
  return axios({
    method: 'delete',
    url: `${base}${url}`,
    params: params,
    headers: {
      'token': accessToken
    }
  });
};

export const getRequest = (url, params) => {
  let accessToken = getStore('accessToken');
  return axios({
    method: 'get',
    url: `${base}${url}`,
    params: params,
    headers: {
      'token': accessToken
    }
  });
};

export const getDefinedRequest = (definedBase, url, params) => {
  let accessToken = getStore('accessToken');
  return axios({
    method: 'get',
    url: `${definedBase}${url}`,
    params: params,
    headers: {
      'token': accessToken
    }
  });
};
